﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HCFramework.API.LINQ;

namespace HCFramework.Product.ProductManage
{
    public partial class InsertUpdate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                litCategoryName.Text = C.ProductCategoryController.GetPathName(M.UrlQuery.categoryID.Value);

                if (M.UrlQuery.copyProductID.HasValue)
                {
                    ChoosePropertyAndFilter1.CopyProductID = M.UrlQuery.copyProductID.Value;
                }
                if (M.UrlQuery.productID.HasValue)
                {
                    ChoosePropertyAndFilter1.ProductID = M.UrlQuery.productID.Value;
                }

                if (M.UrlQuery.productID.HasValue) BindForm(M.UrlQuery.productID.Value);
                if (M.UrlQuery.copyProductID.HasValue) BindForm(M.UrlQuery.copyProductID.Value);
                ChooseManufacturer1.BindCboData();
            }
        }

        private void BindForm(int productId)
        {
            HCFramework.API.LINQ.Product product = C.ProductController.GetByID(productId);
            txtProductCode.Text = product.ProductCode;
            txtProductName.Text = product.ProductName;
            txtQuantity.Text = product.Quantity + "";
            txtPrice.Text = product.Price.ToString("N2");
            txtCurrencyCode.Text = product.CurrencyCode;
            chkIsActive.Checked = product.IsActive;
            RadEditor1.Content = product.Description;
            ChooseManufacturer1.SelectedID = product.ManufacturerID;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            M.Common.GoToPreviousPage();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValid()) return;

            // Table Product
            HCDataContext db = new HCDataContext();
            HCFramework.API.LINQ.Product product = new API.LINQ.Product();
            if (M.UrlQuery.productID.HasValue) product = C.ProductController.GetByID(M.UrlQuery.productID.Value, db);
            product.CategoryID = M.UrlQuery.categoryID.Value;
            product.CurrencyCode = txtCurrencyCode.Text;
            product.Description = RadEditor1.Content;
            product.IsActive = chkIsActive.Checked;
            product.Price = decimal.Parse(txtPrice.Text);
            product.ProductCode = txtProductCode.Text;
            product.ProductName = txtProductName.Text;
            product.Quantity = int.Parse(txtQuantity.Text);
            product.ManufacturerID = ChooseManufacturer1.SelectedID;
            if (M.UrlQuery.productID.HasValue) C.ProductController.Update(product, db);
            else C.ProductController.Insert(product);

            // Table ProductPropertyValue AND Product_PropertyFilter
            ChoosePropertyAndFilter1.Save(product.ProductID);

            M.Common.GoToPreviousPage();
        }

        private bool CheckValid()
        {
            if (txtProductCode.Text == "")
            {
                M.Message.ShowError("ProductCode is required");
                return false;
            }
            if (txtProductName.Text == "")
            {
                M.Message.ShowError("ProductName is required");
                return false;
            }
            if (txtQuantity.Text == "")
            {
                M.Message.ShowError("Quantity is required");
                return false;
            }
            if (!M.Validation.IsValidInt(txtQuantity.Text))
            {
                M.Message.ShowError("Quantity is not valid");
                return false;
            }
            if (txtPrice.Text == "")
            {
                M.Message.ShowError("Price is required");
                return false;
            }
            if (!M.Validation.IsValidDecimal(txtPrice.Text))
            {
                M.Message.ShowError("Price is not valid");
                return false;
            }
            return true;
        }
    }
}